有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何正确使用ArrayList?

好的,所以使用ArrayList对我来说是第一次,我会先咨询我的老师或朋友寻求帮助。。。但这是一门在线课程,所以提问永远都要花时间,到目前为止,我是全校唯一一名选修这门课程的学生(这是一所相当小的学校)

无论如何,这是如此简单,这是荒谬的(我想对你来说),但它摆脱了划线的东西。。。所以我猜我缺少了一个关键部件

public class EmployeeProjectView extends FrameView {

例如,以这条线为例。。。这个程序的开头company是红色下划线的(我正在使用netbeans),我不知道为什么,它可以像String一样正常工作,所以我的问题是。。。。它是否总是必须定义为String

/** Define the ArrayList */
ArrayList<company> employee = new ArrayList<company>();


public EmployeeProjectView(SingleFrameApplication app) {
}// </editor-fold>                        

这(下面是“addButton”)是您在数组中存储信息的位置

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { 

String c;
String ID, firstName, lastName, salary, date;

ID = IDField.getText();
firstName = firstNameField.getText();
salary = annualSalField.getText();
date = startDateField.getText();

新的String以红色下划线

 c = new String(ID, firstName, lastName, salary, date);
    employee.add(c);

}                                         

private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

//Code here gets listed in Field...

    }    

共 (5) 个答案

  1. # 1 楼答案

    它的意思是没有公司的定义,所以它不知道做什么数组列表

    ArrayList<String>它确实理解,因为它知道字符串的定义

    新字符串(ID,…)现在,它正在查看一个构造函数的字符串定义,这个构造函数有五个字符串参数,并且

    你可能还想多想想自己的名字——一个名为employee的公司的集合肯定会把你带到一个糟糕的地方。名为Date和Salary的字符串也不是一件好事

  2. # 2 楼答案

    For example take this line...the beginning of this program, "company" is underlined red (I'm using netbeans) and I'm not sure why, it works fine as "String" so my question is....does it always have to be defined as a String?

    不,去读关于泛型的书。可以将ArrayList定义为采用任何类型。所以只要company是类名,您的声明就可以了。顺便说一下,类名的第一个字母应始终使用大写,以遵循通常的惯例

    从第c = new String(ID, firstName, lastName, salary, date);行判断,您需要了解Java中的对象。字符串是对象的一种类型。请看String类的Javadoc:没有一个构造函数按照您编码的顺序接受其他字符串作为参数。因此,您无法按照您尝试的方式构造新字符串。您似乎对字符串的初始化方式感到困惑。基本上看,您需要阅读一本关于构建对象以及类和方法的Java教程

  3. # 3 楼答案

    ArrayList需要介于< >之间的类型。字符串是一种类型,在您的示例中,公司似乎是一个变量。employee列表应该有什么类型?雇员是字符串列表吗?它是某个用户定义类的列表吗?你需要知道这一点

    http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

    例如:

    ArrayList<UserDefinedClass> myList = 
        new ArrayList<UserDefinedClass>(initialCapacity);
    // initialCapacity should be a previously defined int.
    // Or
    ArrayList<String> someNames = new ArrayList<String>();
    // Uses a default capacity of 10 when none is provided.
    

    其次,String在Java中是一个有趣(但非常方便)的对象。字符串不应该使用new关键字创建,而应该像int{}或short这样的原语一样进行赋值。这是由于一种叫做“字符串实习”的东西,它本身就值得谷歌搜索。要分配字符串,请执行以下操作:

    String word = "Strong Bad";

  4. # 4 楼答案

    也许你想做:

     List<String> c = new ArrayList<String>(5);
     c.add(ID);
     c.add(firstName);
    

    等等

    Arrays类中有一个方便的方法:

    Arrays.asList("Larry", "Moe", "Curly");
    
  5. # 5 楼答案

    下面的内容可能会对你有所帮助

    ArrayList<Company> employee=new ArrayList<Company>();
    
    Company employee1=new Company();
    employee1.setId(id)
    employee1.setFirstname(firstname);
    employee1.setSalary(salary);
    employee1.setDate(dates);
    
    employee.add(employee1);
    

    你一定有这样的公司课程

    class Company {
    String id,firstname,lastname,salary,dates;
    
    //setter   getter method   
    

    (您可以在Netbeans中通过右键单击->;插入代码->;setter和getter来实现这一点)

    干杯